package com.FH.organization.manage_node_update;

import com.FH.organization.manage_line_update.ManageLinePullDTO;
import com.FH.organization.manage_line_update.ManageLineUpdateResource;
import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/11/9.
 */
@Stateless
public class ManageNodeUpdateUpdateDAOImpl extends AccessDB implements ManageNodeUpdateDAO {

    @Inject
    ManageLineUpdateResource manageLineUpdateResource;

    /**
     * 查询节点信息
     *
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getManageNodeInfo(String search, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();
        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(1) from managenode_update where 1=1 ";

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
            }

            if (json.has("nodeid")) {
                sql_w += " AND nodeid = '" + json.getString("nodeid") + "'";
            }
            if (json.has("organization_id")) {
                sql_w += " AND organization_id = '" + json.getString("organization_id") + "'";
            }

            if (json.has("member_id")) {
                sql_w += " AND member_id = '" + json.getString("member_id") + "'";
            }
            if (json.has("step")) {
                sql_w += " AND step = '" + json.getString("step") + "'";
            }
            if (json.has("manage_id")) {
                sql_w += " AND manage_id = '" + json.getString("manage_id") + "'";
            }
            if (json.has("node_state")) {
                sql_w += " AND node_state = '" + json.getString("node_state") + "'";
            }
            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<ManageNodeShowDTO> result = new ArrayList<ManageNodeShowDTO>();
            if (!existName.equals("0")) {
                String sql = "select id,nodeid,nodetext,node_x,node_y,organization_id,member_id,step,manage_id,node_state  from managenode_update  where 1=1 " + sql_w + " ORDER BY step ASC " + sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    ManageNodeShowDTO manageNodeShowDTO = new ManageNodeShowDTO();
                    manageNodeShowDTO.setId(seresult.get(i)[1]);
                    manageNodeShowDTO.setNodeid(seresult.get(i)[2]);
                    manageNodeShowDTO.setNodetext(seresult.get(i)[3]);
                    manageNodeShowDTO.setNode_x(seresult.get(i)[4]);
                    manageNodeShowDTO.setNode_y(seresult.get(i)[5]);
                    manageNodeShowDTO.setOrganization_id(seresult.get(i)[6]);
                    manageNodeShowDTO.setMember_id(seresult.get(i)[7]);
                    manageNodeShowDTO.setStep(seresult.get(i)[8]);
                    manageNodeShowDTO.setManage_id(seresult.get(i)[9]);
                    manageNodeShowDTO.setNode_state(seresult.get(i)[10]);
                    result.add(manageNodeShowDTO);
                }
            }
            if (seresult.size() != 0) {
                resultDTO.setStr(result.size() + "");
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }

            resultDTO.setValues(result);


        } catch (JSONException e) {
            e.printStackTrace();
        }
        return resultDTO;
    }

    /**
     * 添加节点信息
     *
     * @param manageNodePullDTO
     * @return
     */
    @Override
    public ResultDTO addManageNodeInfo(ManageNodePullDTO manageNodePullDTO) {
        ResultDTO resultDTO = new ResultDTO();//构建返回对象

//        String existName = "";
//        String nodeid = manageNodePullDTO.getNodeid();
//        if (nodeid != null && !nodeid.equals("")) {
//
//            String sql = "select count(1) from managenode where nodeid='" + manageNodePullDTO.getNodeid() + "'";
//
//            existName = getReportingToOne(sql);
//
//            if (!existName.equals("0")) {
//                resultDTO.setStatus("90001");
//                resultDTO.setDescritpion("此流程已存在");
//                return resultDTO;
//            }
//        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        String QUERY = "";

        QUERY += "insert into managenode_update(id,nodeid,nodetext,node_x,node_y,organization_id,member_id,step,manage_id,node_state) VALUES('" + uuid + "','" + manageNodePullDTO.getNodeid() + "'" +
                ",'" + manageNodePullDTO.getNodetext() + "','" + manageNodePullDTO.getNode_x() + "','" + manageNodePullDTO.getNode_y() + "','" + manageNodePullDTO.getOrganization_id() + "'" +
                ",'" + manageNodePullDTO.getMember_id() + "','" + manageNodePullDTO.getStep() + "','" + manageNodePullDTO.getManage_id() + "','" + manageNodePullDTO.getNode_state_info() + "')";

        int result = this.addReportingTo(QUERY);
        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStatus("90000");
            System.out.print(QUERY);
            resultDTO.setDescritpion(warnSrc.add_success);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }

    /**
     * 删除节点信息
     *
     * @param manageNodePullDTO
     * @return
     */
    @Override
    public ResultDTO deleteManageNodeInfo(ManageNodePullDTO manageNodePullDTO) {
        //删除之前查看该表是否有记录
        String sql = "select count(1) from managenode_update where manage_id='" + manageNodePullDTO.getManage_id() + "'";

        String existName = getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();

        if (!existName.equals("0")) {

            String QUERY = "";

            QUERY += "delete from managenode_update where manage_id='" + manageNodePullDTO.getManage_id() + "'";

            result = this.addReportingTo(QUERY);
        }
        if (result != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);

            ManageLinePullDTO manageLinePullDTO = new ManageLinePullDTO();
            manageLinePullDTO.setManage_id(manageNodePullDTO.getManage_id());
            manageLineUpdateResource.deleteManageLineInfo(manageLinePullDTO);

        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);

        }
        return resultDTO;

    }
}